package za.co.EYEWTKAH.spring.jms.embedded;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;

public abstract class EmbeddedConnectionFactoryBuilder {

	protected EmbeddedConnectionFactoryConfiguration configuration;
	
	public EmbeddedConnectionFactoryBuilder(EmbeddedConnectionFactoryConfiguration configuration) {
		this.configuration = configuration;
	}

	
	public abstract EmbeddedConnectionFactory getConnectionFactory()
			throws EmbeddedConnectionFactoryException;

	public abstract void shutDown() throws EmbeddedConnectionFactoryException;

	protected EmbeddedConnectionFactory proxy(
			final EmbeddedConnectionFactoryBuilder builder,
			final ConnectionFactory cf) {

		return new EmbeddedConnectionFactory() {

			@Override
			public Connection createConnection(String userName, String password)
					throws JMSException {

				return cf.createConnection(userName, password);
			}

			@Override
			public Connection createConnection() throws JMSException {
				return cf.createConnection();
			}

			@Override
			public void shutDown() throws JMSException {				
				builder.shutDown();
			}
		};
	}

	public static EmbeddedConnectionFactoryBuilder getBuilder(
			EmbeddedConnectionFactoryType type,
			EmbeddedConnectionFactoryConfiguration configuration) {

		switch (type) {
		case ACTIVEMQ:

			return new ActiveMQConnectionFactoryBuilder(configuration);

		case HORNETQ:

			return new HornetQConnectionFactoryBuilder(configuration);

		default:
			break;
		}

		throw new IllegalArgumentException("Type [" + type
				+ "] has no associated connection builder.");
	}
}
